<template>
  <view class="payment">
    <sun-header :title="$t('log.headTitle')"></sun-header>
    <view class="top-bg" :style="'height:' + topHeight + 'px'">
      <image src="/static/images/log/bg.png" mode="aspectFill"></image>
    </view>
    <view>
      <view class="title">
        {{ $t('log.title') }}
      </view>
      <view class="date">
        <view class="item">
          <view class="lable" :style="type == 2 ? 'width:180rpx' : ''">
            <view class="img">
              <image src="/static/images/login/start.svg" mode=""></image>
            </view>
            <view class="">
              {{ type == 1 ? $t('log.start') : $t('log.s') }}
            </view>
          </view>
          <view class="value">
            <picker class="pick" :class="type == 2 ? 'pick2' : ''" mode="date" :value="start" :start="startDate"
              :end="endDate" @change="bindDateChange">
              <view class="uni-input">{{ start }}</view>
            </picker>
            <picker v-if="type == 2" class="pick" :class="type == 2 ? 'pick3' : ''" mode="time" :value="startTime"
              @change="bindTimeChange">
              <view class="uni-input">{{ startTime }}</view>
            </picker>
            <image v-if="type == 1" class="down" src="/static/images/login/arrow.svg" mode=""></image>
          </view>
        </view>
        <view class="item">
          <view class="lable" :style="type == 2 ? 'width:180rpx' : ''">
            <view class="img">
              <image src="/static/images/login/end.svg" mode=""></image>
            </view>
            <view class="">
              {{ type == 1 ? $t('log.end') : $t('log.e') }}
            </view>
          </view>
          <view class="value">
            <picker class="pick" :class="type == 2 ? 'pick2' : ''" mode="date" :value="end" :start="startDate"
              :end="endDate" @change="bindEndChange">
              <view class="uni-input">{{ end }}</view>
            </picker>
            <picker v-if="type == 2" class="pick" :class="type == 2 ? 'pick3' : ''" mode="time" :value="endTime"
              @change="bindEndTimeChange">
              <view class="uni-input">{{ endTime }}</view>
            </picker>
            <image v-if="type == 1" class="down" src="/static/images/login/arrow.svg" mode=""></image>
          </view>
        </view>
      </view>
      <!-- 提示 -->
      <view class="print-tip" v-if="type == 2">
        <view class="p-title">
          <view class="t">{{ $t('PrintTerminalTransactions') }}</view>
          <switch v-model="onlyTerminal" :checked="onlyTerminal" color="#0058FF" @change="switchChange"
            style="transform: scale(0.7)" />
        </view>
        <view class="p-content">
          {{ $t('PrintTerminalTransactionsText') }}
        </view>
      </view>
      <view v-if="type == 1" class="button" @click="findPaymentList">
        {{ $t('QueryTransactionHistory') }}
      </view>
      <view v-if="type == 2" class="button" @click="getPrintDetail">
        <image src="/static/images/login/print-white.svg" mode=""></image>
        {{ $t('log.print') }}
      </view>
      <footer-logo></footer-logo>
    </view>
  </view>
</template>

<script>
  import sunHeader from '@/components/sun-header/sun-header.vue'
  import api from '@/utils/api/index.js'
  import utils from '@/utils/util.js'
  import newPrint from '@/utils/mixins/newPrint.js'
  export default {
    name: 'paymentHistory',
    components: {
      sunHeader,
    },
    mixins: [newPrint],
    data() {
      return {
        start: '',
        bluetoothStatus: false,
        end: '',
        count: 0,
        type: 1,
        startTime: '',
        endTime: '',
        onlyTerminal: true,
        topHeight: 0,
		dcs_base64:'',
		
      }
    },
    computed: {
      startDate() {
        return this.getDate('start')
      },
      endDate() {
        console.log(this.getDate('end'), '')
        return this.getDate('end')
      },
    },
    mounted() {
      this.start = this.getDate('end')
      this.end = this.getDate('end')
      // 默认时分
      let date = new Date()
      let hour = date.getHours()
      let minute = date.getMinutes()
      hour = hour > 9 ? hour : '0' + hour
      minute = minute > 9 ? minute : '0' + minute
      this.startTime = '00:00'
      this.endTime = '23:59'

      // 动态计算高度
      uni.getSystemInfo({
        success: (res) => {
          this.topHeight = res.screenHeight - 450
        },
      })
    },
    onLoad(e) {
      this.type = e.type || 1
      this.onlyTerminal = uni.getStorageSync('onlyTerminal')
      console.log(this.onlyTerminal, 'onlyTerminal')
    },
    methods: {
      switchChange(e) {
        this.onlyTerminal = e.detail.value
        uni.setStorageSync('onlyTerminal', e.detail.value)
      },
      // 查看交易记录
      findPaymentList() {
        let start = this.start + ' ' + this.startTime + ':00'
        let end = this.end + ' ' + this.endTime + ':59'
        uni.navigateTo({
          url: '/pages/paymentHistory/list/list?start=' + start + '&end=' + end,
        })
      },
      async getPrintDetail() {
        console.log('getPrintDetail')
        uni.showModal({
          title: 'Warning',
          content: 'Do you want to print ?',
          cancelText: 'CANCEL',
          confirmText: 'YES',
          success: (res) => {
            if (res.confirm) {
              console.log('用户点击确定')
              this.handlePrint()
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          },
        })
      },
      async handlePrint() {
		let _this = this;
        let start = this.start + ' ' + this.startTime + ':00'
        let end = this.end + ' ' + this.endTime + ':59'
        let o = {
          creationTimeStart: start,
          creationTimeEnd: end,
          printTypeFlag: this.onlyTerminal ? 'terminal' : 'store',
        }
        console.log(o);
        _this.$global.addLog(JSON.stringify(o), "printReceiptSummaryByDateV2");
        let res = await api.printReceiptSummaryByDateV2(o)
        console.log(res)
        _this.$global.addLog(JSON.stringify(res), "printReceiptSummaryByDateV2_result");
        if (res.code == '000000') {
        	this.newPrintTextSummarySunmi(res.data,this.dcs_base64)
        } else {
          utils.showText(res.message)
        }
      },
      bindTimeChange(e) {
        this.startTime = e.detail.value
      },
      bindEndTimeChange(e) {
        this.endTime = e.detail.value
      },

      bindDateChange(e) {
        this.start = e.detail.value
      },
      bindEndChange(e) {
        this.end = e.detail.value
      },
      getDate(type) {
        const date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        month = month > 9 ? month : '0' + month
        day = day > 9 ? day : '0' + day

        if (type === 'start') {
          year = year - 60
        }
        return `${year}-${month}-${day}`
      },
    },
  }
</script>

<style lang="scss" scoped>
  @import 'paymentHistory.scss';
</style>